package com.atguigu.sparkcore.util

import java.sql.{Connection, DriverManager}

import com.atguigu.cm.constan.MysqlCons

/**
 * description ：mysql工具类
 * author ：剧情再美终是戏
 * mail : 13286520398@163.com
 * date ：Created in 2020/1/9 9:31
 * modified By ：
 * version: : 1.0
 */
object MysqlUtil {

  /**
   * 获取mysql连接
   *
   * author 剧情再美终是戏
   * date 2020/1/9 9:34
   *
   * @param dataBase 数据库，默认为defaule
   * @return java.sql.Connection
   *         version 1.0
   **/
  def getConnection(dataBase: String = "default"): Connection = {
    Class.forName(MysqlCons.driver)
    DriverManager.getConnection(MysqlCons.url + dataBase, MysqlCons.user, MysqlCons.passWord)
  }

  /**
   * 关闭mysql连接
   *
   * author 剧情再美终是戏
   * date 2020/1/9 9:35
   *
   * @param conn mysql连接
   * @return void
   *         version 1.0
   **/
  def close(conn: Connection): Unit = conn match {
    case null => None
    case _ => conn.close()
  }
}
